# Contributor: Noel Kuntze <noel.kuntze@thermi.consulting>
# Maintainer: Noel Kuntze <noel.kuntze@thermi.consulting>
pkgname=piler
pkgver=1.4.4
pkgrel=0
pkgdesc="Open source email archiving solution with all the necessary features for your enterprise"
url="https://www.mailpiler.org/"
arch="all !ppc64le !s390x !x86 !armhf !armv7" # exclusions because manticore is not built for these architectures
license="GPL-3.0-only"
depends="
	bash
	catdoc
	coreutils
	findutils
	libzip
	manticore
	memcached
	poppler-utils
	python3
	py3-mysqlclient
	sysstat
	tnef
	tre
	unrtf
	xlhtml
	"
#optional = poppler: for reading attachments
#           catdoc: for reading attachments
#           tnef: for reading attachments
# bash is required to execute the cron maintenance scripts
makedepends="$depends mariadb-dev php82-dev openssl-dev tre-dev libzip-dev"
install="$pkgname.pre-install"
options="!check"
subpackages="$pkgname-openrc"
pkgusers="piler"
pkggroups="piler"
source="https://bitbucket.org/jsuto/piler/downloads/piler-$pkgver.tar.gz
	0001-utf8_encode_char.patch
	0002-manticore-conf.patch
	0003-fix_pilerpurge_for_rt_index.patch

	piler.initd
	piler-smtp.initd
	searchd.initd
	searchd.confd
"

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--sbindir=/usr/bin \
		--localstatedir=/var \
		--libexecdir=/usr/lib \
		--with-database=mysql \
		--with-piler-user=root \
		--enable-memcached

	#Attention! The makefile is not formulated correctly for multithreading! We need to use one thread or it breaks.
	make -j1
}

package() {

	make installdirs DESTDIR="$pkgdir"
	make install DESTDIR="$pkgdir"

	# service files
	rm -rf "$pkgdir"/etc/init.d
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname-smtp.initd "$pkgdir"/etc/init.d/$pkgname-smtp
	install -Dm755 "$srcdir"/searchd.initd "$pkgdir"/etc/init.d/searchd
	install -Dm644 "$srcdir"/searchd.confd "$pkgdir"/etc/conf.d/searchd

	# crontab
	install -Dm600 "$pkgdir"/usr/share/piler/piler.cron "$pkgdir"/etc/crontabs/piler

	# configuration
	install -Dm655 "$builddir"/contrib/webserver/piler-nginx.conf "$pkgdir"/etc/piler/piler-nginx.conf
	install -Dm644 "$builddir"/etc/example.conf "$pkgdir"/etc/piler/piler.conf
	install -Dm644 "$builddir"/etc/smtp.acl.example "$pkgdir"/etc/piler/smtp.acl.example
	install -Dm644 "$builddir"/util/db-upgrade.sql "$pkgdir"/usr/share/piler/db-upgrade.sql
	mv "$pkgdir"/etc/piler/manticore.conf.dist "$pkgdir"/etc/piler/manticore.conf
	mv "$pkgdir"/etc/piler/config-site.dist.php "$pkgdir"/etc/piler/config-site.php
	rm -f "$pkgdir"/etc/piler/sphinx.conf.dist
	touch "$pkgdir"/etc/piler/MANTICORE

	# license
	install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE

	# Fix directory permissions
	chmod -R a-s "$pkgdir"/usr/bin
	chmod 644 "$pkgdir"/etc/piler/*
	chmod 770 "$pkgdir"/var/piler/www/tmp "$pkgdir"/var/piler/www/images
	chown piler:www-data "$pkgdir"/var/piler/www/tmp

	install -d "$pkgdir"/var/log/$pkgname

	# remove unnecessary files
	rm -f "$pkgdir"/usr/lib/piler/*.service "$pkgdir"/usr/lib/piler/postinstall.sh
}

sha512sums="
bd53fa14d01fe50c7fa6ccded4404d4ceb6b19ff4484cc2c4f8fde05cc9a66937c701a4378535afc939707d4c27955ec3f31337674506488b10672fc41ba7b4b  piler-1.4.4.tar.gz
017f3c6241a0368efb6d1b1b734c38b52af67753f5245dad42e265f44961c90bd218d947824a02e7ff02aa11edd642a2947aeff64033f4757670948f88478f4a  0001-utf8_encode_char.patch
6bbf4c70dd51413b6568f2866a28524eb2986fee35fc4921166baa702fbf466ded7c3357ef7987403c3ad694bbd62aad9abfc90ac8313613e31b7b65784c9b08  0002-manticore-conf.patch
aefef3ff8101c62524f3cd866e3243d4e0cfb2acfd98627db29d9e2d8c12647962bf1b7b62dbd8d0f8b8b20f7719da134dde5d903b4adcf589f5494576ee3e58  0003-fix_pilerpurge_for_rt_index.patch
f3895913350ffb252455c50bb276cc9fff0603804507d2a3f6ab6167a1b8b357ca8039ccb8725849ffa99fe04673d510dfcdc09f099631a8f0dd148494def615  piler.initd
3aa8e661c1c806311abecdc76dcbd12a167e64de11785d2a3c63416cd1e5a445eb021ff4882de5fe1a145fc78483b3691262a07fc0168bbfc03b88812cc1ad17  piler-smtp.initd
c4ad4742c3bd899d8f8004f16252b4862e19683cba6d3d4a7c86d9ed90942c7b3878fc713915aa299b28852f798fedee5ae73e28844d653383d6c7a07698fde4  searchd.initd
d6a57f4f972786898d39e94666daa36ba6265dbb577d97e4a9586d4e7b2f3f7b4e69537b316529123186df30f530dc54c03af10c88c8e205192d2b8568571bb3  searchd.confd
"
